module main;

import DSFMLGame;

import dsfml.system.clock;

import tango.core.Thread;

static this()
{
    version (Dynamic)
    {
        dsfml.system.loader.init();
        dsfml.window.loader.init();
        dsfml.graphics.loader.init();
        dsfml.audio.loader.init();
    }
}

public void main()
{
	auto game = new DSFMLGame();
	game.init();
	scope(exit) game.deinit();
	
	final preferredFPS = 60;
	long preferredTicksPerFrame = 1 / preferredFPS;
	
	Clock clock = new Clock();
	float lastFrame = 0.0f;
	
	while (game.isRunning())
	{
		float frameStart = clock.getElapsedTime();

		float tpf = frameStart - lastFrame;
		game.update(tpf);
		game.draw(tpf);

		float frameDuration = clock.getElapsedTime() - frameStart;
		while (frameDuration < preferredTicksPerFrame)
		{
			Thread.sleep((preferredTicksPerFrame - frameDuration) * 
				0.001);
			frameDuration = clock.getElapsedTime() - frameStart;
		}

		Thread.yield();
		lastFrame = frameStart;
	}
}
